package ThreadPool;

import java.util.concurrent.CompletableFuture;

public class CompletableFutureTest {


    public static void main(String[] args) throws Exception{

        // 创建第一个线程，执行1-5相加
        CompletableFuture<Integer> completableFuture1 = CompletableFuture.supplyAsync(()->{

            int sum=0,i=0;
            while(i++<5){
                sum+=i;
                System.out.println(Thread.currentThread().getName()+"线程正在执行...i"+i);
            }
            return sum;
        });

        // 创建第二个线程，执行6-10相加
        CompletableFuture<Integer> completableFuture2 = CompletableFuture.supplyAsync(()->{

            int sum=0,j=5;
            while (j++<10){
                sum+=j;
                System.out.println(Thread.currentThread().getName()+"线程正在执行...j"+j);
            }
            return sum;
        });

        // 将两个线程运算结果进行取整
        CompletableFuture<Integer> completableFuture3 = completableFuture1.thenCombine(completableFuture2,(result1,result2)->result1+result2);
        System.out.println("1到10相加的结果为："+completableFuture3.get());

    }

}
